package com.ruoyi.neo.repository;

import com.ruoyi.neo.domain.PartCategory;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PartCategoryRepository extends Neo4jRepository<PartCategory,Long> {
    // 查询所有分类

    //条件查询
    @Query("MATCH (c:PartCategory) " +
            "WHERE ($categoryName IS NULL OR c.categoryName CONTAINS $categoryName) " +
            "  AND ($parentCategoryId IS NULL OR c.parentCategoryId = $parentCategoryId) " +
            "  AND ($description IS NULL OR c.description = $description) " +
            "RETURN c")
    List<PartCategory> findAllCategoriesInNeo4j(@Param("categoryName") String categoryName,
                                                @Param("parentCategoryId") Long parentCategoryId,
                                                @Param("description") String description);
    // 根据分类ID查询分类
    @Query("MATCH (c:PartCategory) WHERE c.categoryId = $categoryId RETURN c")
    PartCategory findCategoryByIdInNeo4j(@Param("categoryId") Long categoryId);

    // 根据父分类ID查询子分类
    @Query("MATCH (c:PartCategory) WHERE c.parentCategoryId = $parentCategoryId RETURN c")
    List<PartCategory> findCategoriesByParentIdInNeo4j(@Param("parentCategoryId") Long parentCategoryId);

    // 添加分类
    @Query("CREATE (c:PartCategory {categoryId: $categoryId,categoryName: $categoryName, parentCategoryId: $parentCategoryId, description: $description}) RETURN id(c)")
    Long addCategoryToNeo4j(@Param("categoryId") Long categoryId,@Param("categoryName") String categoryName,
                     @Param("parentCategoryId") Long parentCategoryId,@Param("description")String description);

    // 更新分类
    @Query("MATCH (c:PartCategory) WHERE c.categoryName = $oldName SET c.categoryName = $categoryName, c.parentCategoryId = $parentCategoryId, c.description = $description RETURN COUNT(c)")
    int updateCategoryToNeo4j(@Param("oldName") String oldName,@Param("categoryName") String categoryName,@Param("parentCategoryId") Long parentCategoryId,@Param("description")String description);

    // 按照nameList删除零件分类
    @Query("MATCH (c:PartCategory) WHERE c.categoryName IN $nameList DETACH DELETE c RETURN count(c)")
    int deleteCategoryInNeo4j(@Param("nameList") String[] nameList);
}
